/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Jun 15, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.hce.dendroview.gui.paint;



import epoviz.trex.common.text.JTextUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import java.util.List;
public class HMObservation implements HLObject{
    private Color color=Color.black ;
    private Rectangle regoin;//  

    private String id;
    private int index;
    private List attrLabels;// 

    public HMObservation(Rectangle regoin) {
        this.regoin = regoin;
    }

    public void paint(Graphics g) {
         Graphics2D g2d=(Graphics2D) g;
         g2d.draw(regoin);


        int x = regoin.x;
        int y = regoin.y-5;


        String obname = "obsr:"+index;
        if(this.id !=null){
             obname=obname+" ("+id+")";
        }
        /*Font font = g2d.getFont();
        Rectangle2D bound = JTextUtils.getTextBound(obname,font,g2d);
        int w = (int)bound.getWidth()+10;
        int h = (int)bound.getHeight()+10;
        Rectangle lbregoin=new Rectangle(x,y-h+5,w,h);//label regoin
        g2d.setColor(Color.yellow );

        g2d.fill(lbregoin);
        g2d.setColor(Color.black );
        g2d.drawString(obname,x, y);*/
        paintBgString(g2d,obname,x,y);
        paintAttrLabels(g);
    }

    private void paintAttrLabels(Graphics g) {
        Graphics2D g2d=(Graphics2D) g;
        if(attrLabels!=null){
            for (Iterator iterator = attrLabels.iterator(); iterator.hasNext();) {
                JLabel lb = (JLabel) iterator.next();
                /*lb.setPreferredSize(new Dimension(40,20));
                lb.setSize(40,20);       */
                Point loc = lb.getLocation();
                String text = lb.getText();

               // paintBgString(g2d,text,loc.x,loc.y);
                if(text!=null)
                g.drawString(text,loc.x,loc.y);
                //lb.setOpaque(true);
                //lb.paint(g);
            }
        }
    }
    /**
     * paint string with background
     * @param text
     * @param x
     * @param y
     */
    private void paintBgString(Graphics2D g2d,String text,int x, int y){
        Font font = g2d.getFont();
        Rectangle2D bound = JTextUtils.getTextBound(text,font,g2d);
        int xextra=10;//extra space assign to backgound of a text
        int yextra=10;
        int w = (int)bound.getWidth()+xextra;
        int h = (int)bound.getHeight()+yextra;
        Rectangle lbregoin=new Rectangle(x-xextra/2,y-h+yextra/2,w,h);//label regoin
        g2d.setColor(Color.white );

        g2d.fill(lbregoin);
        g2d.setColor(Color.black );
        g2d.drawString(text,x, y);
       // paintAttrLabels(g2d);
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public void setRegoin(Rectangle regoin) {
        this.regoin = regoin;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public void setAttrLabels(List attrLabels) {
        this.attrLabels = attrLabels;
    }
}
